package fact.view.controller;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;
import javax.servlet.http.HttpSession;

import com.icesoft.faces.context.effects.JavascriptContext;

import fact.view.DTO.MarketDTO;
import fact.view.model.MarketModelBean;
import fact.view.util.FacesUtils;
import fact.view.util.ServletUtil;


@ManagedBean(name = "marketBean")
@RequestScoped
public class MarketBean extends ServletUtil implements Serializable {

	private static final long serialVersionUID = 8292853620260831727L;
	private boolean opened = false;
	private boolean editOpened = false;
	
	
	@ManagedProperty(value="#{marketModelBean}")
	private MarketModelBean marketModelBean;
	
	ArrayList<MarketDTO> marketList=new  ArrayList<MarketDTO>();
	private SelectItem[] marketSelectList;

	private MarketDTO market=new MarketDTO();
	private MarketDTO editMarket=new MarketDTO();
		 
    HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		
	public String addMarket(ActionEvent event) throws SQLException
	{
		editOpened=false;
		opened=false;
		
		if(this.market.getMarketName()==null || this.market.getMarketName().trim().equalsIgnoreCase(""))
		{
			opened=true;
			
			FacesUtils.addErrorMessage("form:marketName","Market Name cannot be empty.");
		}
		if(marketModelBean.getMarket(this.market.getMarketName().trim())!=null)
		{
			opened=true;
			
			FacesUtils.addErrorMessage("form:marketName","Market Name Already Exist.");
		}
		if(opened==false)
		{
			if(marketModelBean.addMarket(this.market.getMarketName())==true)
			{
				marketList=marketModelBean.getMarketList();
				session.setAttribute("session_marketList", marketList);
				
				FacesUtils.addInfoMessage(null,"Market has successfully been added.");

			}
			return "admin/market.jsf";
		}
		
		return null;
	}
	
	public String deleteMarket() throws SQLException
	{
		System.out.println("Market Id :"+this.market.getMarketId());
		if(marketModelBean.deleteMarket(this.market.getMarketId())==true)
		{
			marketList=marketModelBean.getMarketList();
			session.setAttribute("session_marketList", marketList);
			
			FacesUtils.addInfoMessage(null,"Market has successfully been deleted.");
		}
		return "admin/market.jsf";
	}
	
	 
	public String editMarket() throws SQLException
	{
		System.out.println("Market Id :"+this.editMarket.getMarketId());
		
		editOpened=false;
		opened=false;
		
		if(this.editMarket.getMarketName()==null || this.editMarket.getMarketName().trim().equalsIgnoreCase(""))
		{
			editOpened=true;
			
			FacesUtils.addErrorMessage("form:editMarketName","Market Name cannot be empty.");
		}
		
		if(editOpened==false)
		{
			if(marketModelBean.editMarket(this.editMarket.getMarketName(),this.editMarket.getMarketId())==true)
			{
				marketList=marketModelBean.getMarketList();
				session.setAttribute("session_marketList", marketList);
				
				FacesUtils.addInfoMessage(null,"Market has successfully been edited.");
				return "admin/market.jsf";
			}
			
		}
		return null;
		
	}

	
	public MarketModelBean getmarketModelBean() {
		return marketModelBean;
	}



	public void setmarketModelBean(MarketModelBean marketModelBean) {
		this.marketModelBean = marketModelBean;
	}

	public ArrayList<MarketDTO> getMarketList() {
		try{
			
			if(session.getAttribute("session_marketList")==null)
			{
				marketList=marketModelBean.getMarketList();
				session.setAttribute("session_marketList", marketList);
			}
			else
				marketList=(ArrayList<MarketDTO>)session.getAttribute("session_marketList");
		   }
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
		return marketList;
	}
	public void setMarketList(ArrayList<MarketDTO> marketList) {
		this.marketList = marketList;
	}

	
	public MarketDTO getMarket() {
		return market;
	}

	public void setMarket(MarketDTO market) {
		this.market = market;
	}
	
    

	public MarketDTO getEditMarket() {
		return editMarket;
	}


	public void setEditMarket(MarketDTO editMarket) {
		this.editMarket = editMarket;
	}


	public SelectItem[] getMarketSelectList() {
		
		if(marketList.size()==0)
			getMarketList();
		
		marketSelectList = new SelectItem[marketList.size()];
		 for(int i=0;i<marketList.size();i++)
			 marketSelectList[i] = new SelectItem(marketList.get(i).getMarketId(), marketList.get(i).getMarketName());
		 
		 
		return marketSelectList;
	}

	public void setMarketSelectList(SelectItem[] marketSelectList) {
		
		
		this.marketSelectList = marketSelectList;
	}

	/** -- Popup Modal Event ---**/
		public boolean isOpened() { return opened; }
		public boolean isEditOpened() { return editOpened; }
	    
	    public void setOpened(boolean opened) { this.opened = opened; }
	    public void setEditOpened(boolean editOpened) { this.editOpened = editOpened; }
	    
	    
	    public void openEvent(ActionEvent event) {
	    	this.market=null;
	        opened = true;
	        editOpened = false;
	        
	        FacesContext facesContext=FacesContext.getCurrentInstance();
	        JavascriptContext.addJavascriptCall(facesContext, "setTimeout(\"changeFocus()\", 100);"); 
	    }
	    public void openEditEvent(ActionEvent event)  throws SQLException{
	    	this.editMarket=marketModelBean.getMarket(this.editMarket.getMarketId());
	    	editOpened = true;
	    	opened = false;
	    }
	    
	    
	    public void closeEvent(ActionEvent event) {
	        opened = false;
	        editOpened = false;
	    }

    /*  End of Modal */
	

}
